/*
Copyright (C) 2008 Matthew Perry

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;

namespace BaseTools.IO.Binary
{
	
	/// <summary>
	/// File IByteReader
	/// </summary>
	[Obsolete()]
	public class FileByteReader : IByteReader
	{
		FileStream fstream;
		bool ac;
		
		/// <summary>
		/// Creates a new FileByteReader
		/// </summary>
		/// <param name="fstream">The filestream to use</param>
		public FileByteReader(FileStream fstream)
		{
			this.fstream = fstream;
			ac = false;
		}
		
		/// <summary>
		/// Creates a new FileByteReader
		/// </summary>
		/// <param name="filename">The filename to use</param>
		public FileByteReader(string filename)
		{
			this.fstream = new FileStream(filename, FileMode.Open);
			ac = true;
		}
		
		/// <summary>
		/// Closes this stream
		/// </summary>
		public void Close()
		{
			fstream.Close();
		}
		
		/// <summary>
		/// Closes the stream if initialized by filename
		/// </summary>
		~FileByteReader()
		{
			if (ac)
				fstream.Close();
		}
		
		/// <summary>
		/// Seeks to the position in the file
		/// </summary>
		/// <param name="pos">The position</param>
		/// <param name="origin">The origin of the position</param>
		public void Seek(long pos, SeekPos origin)
		{
			SeekOrigin ro = SeekOrigin.Begin;
			
			switch(origin)
			{
				case SeekPos.Begin:
					ro = SeekOrigin.Begin;
					break;
				case SeekPos.Current:
					ro = SeekOrigin.Current;
					break;
				case SeekPos.End:
					ro = SeekOrigin.End;
					break;
			}
			
			fstream.Seek(pos, ro);
		}
		
		/// <summary>
		/// Read bytes from the file
		/// </summary>
		/// <param name="size">The number of bytes to read</param>
		/// <returns>The bytes</returns>
		public byte[] ReadBytes(int size)
		{
			byte[] by = new byte[size];
			
			fstream.Read(by, (int)fstream.Position, size);
			
			return by;
		}
		
		/// <summary>
		/// Read a byte from the file
		/// </summary>
		/// <returns>A byte from the file</returns>
		public byte ReadByte()
		{
			return (byte)fstream.ReadByte();
		}
	
	}
}
